﻿using UnityEngine;
using System.Collections;
using OhioState.Tiling;

public class GameObjTileSet : ITileSet<GameObject>
{
    public enum Edge
    {
        North,
        South,
        East,
        West
    }

    public int NumberOfTiles { get; private set; }
    public Texture2D[,] Tiles;
    public bool[, ,] Edges;
    public Texture2D RawTileMap;

    public GameObjTileSet(Texture2D map, TileIndex2D dim, int edgesPerTile)
    {
        NumberOfTiles = dim.X * dim.Z;
        RawTileMap = map;
        Tiles = new Texture2D[dim.X, dim.Z];
        Edges = new bool[dim.X, dim.Z, 4];
    }

    public ITile<GameObject> GetTile(int index)
    {
        throw new System.NotImplementedException();
    }

    public static GameObjTileSet.Edge GetNumericEdge(int i)
    {
        GameObjTileSet.Edge edge = GameObjTileSet.Edge.North;
        if (i == 1) edge = GameObjTileSet.Edge.East;
        if (i == 2) edge = GameObjTileSet.Edge.South;
        if (i == 3) edge = GameObjTileSet.Edge.West;

        return edge;
    }

    public static int GetNumericEdge(GameObjTileSet.Edge edge)
    {
        int i = 0;
        if (edge == GameObjTileSet.Edge.East) i = 1;
        if (edge == GameObjTileSet.Edge.South) i = 2;
        if (edge == GameObjTileSet.Edge.West) i = 3;

        return i;
    }

    public static GameObjTileSet.Edge GetAdjacentEdge(Edge edge)
    {
        int i = GetNumericEdge(edge);
        i = (i + 2) % 4;
        return GetNumericEdge(i);
    }

    public GameObject CreateGameObjectTile(TileIndex2D index)
    {
        GameObject tile = GameObject.CreatePrimitive(PrimitiveType.Plane);
        Material m = new Material(tile.renderer.material);
        m.SetTexture(0, Tiles[index.X, index.Z]);
        tile.renderer.material = m;
        return tile;
    }
}